﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Exam.Controllers
{
    [AllowAnonymous]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AdminController : ControllerBase
    {
        private static System.Text.StringBuilder m_Resp = new System.Text.StringBuilder();
        [HttpPost]
        public async Task<IActionResult> CreateDB([FromServices] Exam.DBManager.IDbManager dbManager)
        {
            m_Resp.AppendLine("开始创建数据库......");

            dbManager.Exam.Database.EnsureDeleted();
            dbManager.Exam.Database.EnsureCreated();
            m_Resp.AppendLine("................................数据库创建成功.");
            await InitData(dbManager);

            return Content(m_Resp.ToString());
        }
        private async Task InitData(Exam.DBManager.IDbManager dbManager)
        {
            m_Resp.AppendLine("初始化用户账号");
            var administrator = new Exam.Model.SysUser()
            {
                account = "admin",
                password = "123456",
                phone = "124555244",
                state = true,
            };

            dbManager.Exam.SysUsers.AddRange(administrator);
            dbManager.Exam.SaveChanges();
        }
    }
}
